﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.Win32.SafeHandles;
using NAudio.Wave;

namespace HPKaraoke
{
    public class XmlSongController : SongController
    {
        WaveChannel32 volumeStream;
        bool wasLoaded = false;

        private WaveStream CreateInputStream(string fileName)
        {
            WaveChannel32 inputStream;
            //if (fileName.EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(fileName);
                inputStream = new WaveChannel32(mp3Reader);
            }

            volumeStream = inputStream;
            return volumeStream;
        }

        /// <summary>
        /// Khởi tạo bài hát với dữ liệu:
        ///     Lyrics lấy từ trong file có đường dẫn LyricFileName
        ///     Nhạc beat lấy từ file có đường dẫn BeatFileName
        /// </summary>
        /// <param name="LyricFileName">Đường dẫn đến file lời nhạc</param>
        /// <param name="BeatFileName">String chứa đường dẫn đến file beat</param>
        public void LoadXmlSong(string LyricFileName, string BeatFileName)
        {
            BeatMusicUri = new Uri(BeatFileName, UriKind.Relative);
            XmlDocument doc = new XmlDocument();
            doc.Load(LyricFileName);
            XmlNodeList nodeList = doc.SelectNodes(@"//param");
            Lyrics = new List<Sentence>();

            int count = 0;
            foreach (var sentenceXML in nodeList)
            {
                Sentence s = new Sentence(((XmlElement)sentenceXML).OuterXml);
                Lyrics.Add(s);
                count++;
                if (count >= 2)
                {
                    Lyrics[count - 2].Duration = Lyrics[count - 1].TimeToStart - Lyrics[count - 2].TimeToStart;
                }
            }

            int temp = Lyrics[count-1].Words.Count - 1;
            Lyrics[count - 1].Duration = Lyrics[count - 1].Words[temp].Time - Lyrics[count - 1].Words[0].Time + Lyrics[count - 1].Words[temp].Time - Lyrics[count - 1].Words[temp - 1].Time;
        }

        override public void LoadInputStream()
        {
            InputStream = CreateInputStream(this.BeatMusicUri.OriginalString);
            wasLoaded = true;
        }

        public XmlSongController(string ParentPath, string xmlConfigFileName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlConfigFileName);

            this.Information.ID = int.Parse(doc.SelectSingleNode(@"//id").InnerText.Trim());

            string LyricFileName = ParentPath + doc.SelectSingleNode(@"//lyric").InnerText.Substring(8);
            string BeatFileName = ParentPath + doc.SelectSingleNode(@"//karaokelink").InnerText.Substring(8);

            this.Information.Title = doc.SelectSingleNode(@"//title").InnerText;
            this.Information.Singer = doc.SelectSingleNode(@"//singer").InnerText;
            this.Information.Author = doc.SelectSingleNode(@"//author").InnerText;

            LoadXmlSong(LyricFileName, BeatFileName);

        }

        override public void Close()
        {
            volumeStream.Close();
            volumeStream = null;
            (InputStream as WaveStream).Close();
            (InputStream as WaveStream).Dispose();
        }
    }
}
